package com.yyyzzz.servlet;

import com.yyyzzz.bean.Student;
import com.yyyzzz.service.impl.studentServiceimpl;
import com.yyyzzz.service.studentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/studentLoginServlet")
public class studentLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 public studentLoginServlet(){
    super();
}
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String stuNo = request.getParameter("stuNo");
        String stuPwd = request.getParameter("stuPwd");

        Student student = new Student(stuNo,stuPwd);

        // 登录业务
        studentService adminService = new studentServiceimpl();
        boolean isLogin = adminService.isLogin(student);

        if(isLogin== true){
            HttpSession session = request.getSession();
            session.setAttribute("id",stuNo);
            session.setAttribute("student",student);

            //response.getWriter().append("SUCCESS");
            //跳转到manager.html
//			请求转发（绝大部分）
            request.getRequestDispatcher("studentMain.jsp").forward(request, response);
//			请求重定向
        }else{
            request.setAttribute("error","用户名或密码错误" );
            request.getRequestDispatcher("studentLogin.jsp").forward(request, response);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
